
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>如何在 Windows 上安装 Django &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="编写数据库迁移语句" href="writing-migrations.html" />
    <link rel="prev" title="部署静态文件" href="static-files/deployment.html" />



 
<script src="../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="static-files/deployment.html" title="部署静态文件">previous</a>
     |
    <a href="index.html" title="操作指南" accesskey="U">up</a>
   |
    <a href="writing-migrations.html" title="编写数据库迁移语句">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="howto-windows">
            
  <div class="section" id="s-how-to-install-django-on-windows">
<span id="how-to-install-django-on-windows"></span><h1>如何在 Windows 上安装 Django<a class="headerlink" href="#how-to-install-django-on-windows" title="永久链接至标题">¶</a></h1>
<p>本文是安装在 Windows 上安装 Python 3.8 和 Django 的指南。同时也提供了关于配置虚拟环境的介绍，它们使得编写 Python 工程变的非常简单。这是为从事 Django 项目的用户提供的入门指南，而介绍为 Django 本身开发补丁时该如何安装 Django。</p>
<p>指南中的下列步骤已在 Windows 10上测试通过。其它版本上，步骤类似。你需要熟悉如何使用 Windows 的命令提示符。</p>
<div class="section" id="s-install-python">
<span id="s-install-python-windows"></span><span id="install-python"></span><span id="install-python-windows"></span><h2>安装Python<a class="headerlink" href="#install-python" title="永久链接至标题">¶</a></h2>
<p>Django 是一个 Python Web 框架，因此需要在您的机器上安装 Python。在本文撰写时，Python 最新的版本是 3.8。</p>
<p>To install Python on your machine go to <a class="reference external" href="https://www.python.org/downloads/">https://www.python.org/downloads/</a>. The
website should offer you a download button for the latest Python version.
Download the executable installer and run it. Check the boxes next to &quot;Install
launcher for all users (recommended)&quot; then click &quot;Install Now&quot;.</p>
<p>安装后，打开命令提示符，检查 Python 版本是否与你装的一致，通过运行:</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py --version
</pre></div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<p class="last">想知晓更多细节，请查看 <a class="reference external" href="https://docs.python.org/3/using/windows.html" title="(在 Python v3.9)"><span>Using Python on Windows</span></a> 文档。</p>
</div>
</div>
<div class="section" id="s-about-pip">
<span id="about-pip"></span><h2>关于 <code class="docutils literal notranslate"><span class="pre">pip</span></code><a class="headerlink" href="#about-pip" title="永久链接至标题">¶</a></h2>
<p><a class="reference external" href="https://pypi.org/project/pip/">pip</a> 是 Python 的包管理器并且默认就在Python安装包里。它使得 Python 包（例如 Django）的安装和卸载变的非常简单。剩下的安装流程中，我们会用 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 从命令行安装 Python 包。</p>
</div>
<div class="section" id="s-setting-up-a-virtual-environment">
<span id="s-virtualenvironment"></span><span id="setting-up-a-virtual-environment"></span><span id="virtualenvironment"></span><h2>配置一个虚拟环境<a class="headerlink" href="#setting-up-a-virtual-environment" title="永久链接至标题">¶</a></h2>
<p>最佳实践是为你创建的每一个Django项目创建一个独立的环境。在Python生态系统中有许多可以用来管理环境和包的可选工具，一些在 <a class="reference external" href="https://packaging.python.org/guides/tool-recommendations/">Python documentation</a> 被推荐过。我们将会在这份指南中使用Python本身自带的 <a class="reference external" href="https://docs.python.org/3/tutorial/venv.html" title="(在 Python v3.9)"><span class="xref std std-doc">venv</span></a>  来管理环境。</p>
<p>要为你的项目创建一个虚拟环境，打开新的命令行窗口，转至你想创建项目的文件夹，然后输入如下命令</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py -m venv project-name
</pre></div>
</div>
<p>这样就会创建一个名字为“project-name”文件夹并且设置好相应的虚拟环境。为了激活环境，请执行如下命令</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> project-name\Scripts\activate.bat
</pre></div>
</div>
<p>虚拟环境就会激活，并且你会在命令提示符旁边看到 &quot;(project-name)&quot; 指向该环境。每次你打开新的命令行窗口，你需要再次激活环境。</p>
</div>
<div class="section" id="s-install-django">
<span id="install-django"></span><h2>安装 Django<a class="headerlink" href="#install-django" title="永久链接至标题">¶</a></h2>
<p>Django 可以轻松地在你的虚拟环境中使用 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 安装。</p>
<p>在命令提示行中，确认虚拟环境是激活的，然后运行以下命令:</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py -m pip install Django
</pre></div>
</div>
<p>这将下载并安装最新的 Django 发布版本。</p>
<p>安装完成后，你可以在命令提示符运行 <code class="docutils literal notranslate"><span class="pre">django-admin</span> <span class="pre">--version</span></code> 验证你安装的 Django。</p>
<p>参考 <span class="xref std std-ref">数据库安装</span> 了解如何通过 Django 安装数据库。</p>
</div>
<div class="section" id="s-colored-terminal-output">
<span id="colored-terminal-output"></span><h2>Colored terminal output<a class="headerlink" href="#colored-terminal-output" title="永久链接至标题">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>A quality-of-life feature adds colored (rather than monochrome) output to the
terminal. In modern terminals this should work for both CMD and PowerShell. If
for some reason this needs to be disabled, set the environmental variable
<span class="target" id="index-0"></span><a class="reference internal" href="../ref/django-admin.html#envvar-DJANGO_COLORS"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_COLORS</span></code></a> to <code class="docutils literal notranslate"><span class="pre">nocolor</span></code>.</p>
<p>On older Windows versions, or legacy terminals, <a class="reference external" href="https://pypi.org/project/colorama/">colorama</a> must be installed to
enable syntax coloring:</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py -m pip install colorama
</pre></div>
</div>
<p>See <a class="reference internal" href="../ref/django-admin.html#syntax-coloring"><span class="std std-ref">语法着色</span></a> for more information on color settings.</p>
</div>
<div class="section" id="s-common-pitfalls">
<span id="common-pitfalls"></span><h2>常见失误<a class="headerlink" href="#common-pitfalls" title="永久链接至标题">¶</a></h2>
<ul>
<li><p class="first">若 <code class="docutils literal notranslate"><span class="pre">django-admin</span></code> 无论输入啥参数都只显示帮助信息，这可能是一个 Windos 的文件关联问题。检查 <code class="docutils literal notranslate"><span class="pre">PATH</span></code> 中是否不止一个用于运行 Python 脚本的环境变量。该问题通常在安装了多个 Python 版本时出现。</p>
</li>
<li><p class="first">若你通过代理来连接互联网，运行命令 <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">Django</span></code> 时可能会出现问题。在命令提示符中为代理配置如下环境变量:</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> <span class="k">set</span> <span class="nv">http_proxy</span><span class="p">=</span>http://username:password@proxyserver:proxyport
<span class="gp">...\&gt;</span> <span class="k">set</span> <span class="nv">https_proxy</span><span class="p">=</span>https://username:password@proxyserver:proxyport
</pre></div>
</div>
</li>
<li><p class="first">In general, Django assumes that <code class="docutils literal notranslate"><span class="pre">UTF-8</span></code> encoding is used for I/O. This may
cause problems if your system is set to use a different encoding. Recent
versions of Python allow setting the <span class="target" id="index-1"></span><a class="reference external" href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUTF8" title="(在 Python v3.9)"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> environment
variable in order to force a <code class="docutils literal notranslate"><span class="pre">UTF-8</span></code> encoding. Windows 10 also provides a
system-wide setting by checking <code class="docutils literal notranslate"><span class="pre">Use</span> <span class="pre">Unicode</span> <span class="pre">UTF-8</span> <span class="pre">for</span> <span class="pre">worldwide</span> <span class="pre">language</span>
<span class="pre">support</span></code> in <span class="menuselection">Language ‣ Administrative Language Settings
‣ Change system locale</span> in system settings.</p>
</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">如何在 Windows 上安装 Django</a><ul>
<li><a class="reference internal" href="#install-python">安装Python</a></li>
<li><a class="reference internal" href="#about-pip">关于 <code class="docutils literal notranslate"><span class="pre">pip</span></code></a></li>
<li><a class="reference internal" href="#setting-up-a-virtual-environment">配置一个虚拟环境</a></li>
<li><a class="reference internal" href="#install-django">安装 Django</a></li>
<li><a class="reference internal" href="#colored-terminal-output">Colored terminal output</a></li>
<li><a class="reference internal" href="#common-pitfalls">常见失误</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="static-files/deployment.html"
                        title="上一章">部署静态文件</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="writing-migrations.html"
                        title="下一章">编写数据库迁移语句</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/howto/windows.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="static-files/deployment.html" title="部署静态文件">previous</a>
     |
    <a href="index.html" title="操作指南" accesskey="U">up</a>
   |
    <a href="writing-migrations.html" title="编写数据库迁移语句">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>